#!/bin/bash
echo "开始执行脚本"
supervisord -c /supervisord.conf
#根据COMMAND传来的参数做判断
echo "启动redis完毕"
rb_args=''
if [ "$1" = 'redis-cluster' ]; then
    # args=$(($#-1))
    # echo "$args"
    #等待docker初始化ip
    sleep 30
    for ((i=2; i<=$#; i ++))
    do
    	eval b=\$$i
    	hostnames=`echo "$b" | awk -F ":" '{print $1}' `
    	#获取域名对应的IP地址
    	ip=`ping -c 1 $hostnames| grep 'PING' | awk  'match($0,/\(.*?\)/) {print substr($0,RSTART,RLENGTH);}' |cut -d  " " -f 1 | sed 's/[\(\)]//g'`
    	port=`echo $b | cut -d ":" -f 2`
    	rb_args=$rb_args" $ip:$port"
    	#
    done
    echo $rb_args
    echo "集群初始化"
    echo "yes" | /redis-stable/src/redis-trib.rb create $rb_args
    echo "集群初始化完毕"

fi
sleep 15
tail -f /redis.log
